package com.wsc.wsckeeper.adapter;

import java.text.SimpleDateFormat;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wsc.wsckeeper.R;
import com.wsc.wsckeeper.components.CardNumberTextView;
import com.wsc.wsckeeper.managers.ResourceManager;
import com.wsc.wsckeeper.model.entity.CardE;

@SuppressLint("SimpleDateFormat")
public class CardsAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private List<CardE> cards;
	private SimpleDateFormat sdf;

	public CardsAdapter(Context context, List<CardE> cards) {
		this.cards = cards;
		sdf = new SimpleDateFormat(ResourceManager.getStringValue(R.string.format_history_date));
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;

		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.adapter_item_card, null);
			holder.cardCurrency = (ImageView) convertView.findViewById(R.id.item_card_currency);
			holder.cardType = (ImageView) convertView.findViewById(R.id.item_card_type);
			holder.cardNumber = (CardNumberTextView) convertView.findViewById(R.id.item_card_number);
			holder.cardDescription = (TextView) convertView.findViewById(R.id.item_card_description);
			holder.created = (TextView) convertView.findViewById(R.id.item_created);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		CardE item = cards.get(position);
		if (item != null) {
			holder.created.setText(sdf.format(item.getCreated()));
			holder.cardNumber.setText(item.getNumber());
			holder.cardDescription.setText(item.getDescription());
			// if (item.getCardType() != null) {
			holder.cardType.setImageResource(item.getCardType().getImage());
			holder.cardCurrency.setImageResource(item.getCurrency().getImage());
			// } else {
			// int type = Integer.parseInt("" +
			// item.getNumber().toString().charAt(0));
			// switch (type) {
			// case 4:
			// holder.cardType.setImageResource(R.drawable.card_visa);
			// break;
			// case 5:
			// holder.cardType.setImageResource(R.drawable.card_master);
			// break;
			// case 3:
			// case 6:
			// holder.cardType.setImageResource(R.drawable.card_maestro);
			// break;
			// default:
			// holder.cardType.setImageResource(R.drawable.card_unknown);
			// }
			// }
		}
		return convertView;
	}

	class ViewHolder {
		ImageView cardType;
		ImageView cardCurrency;
		CardNumberTextView cardNumber;
		TextView cardDescription;
		TextView created;
	}

	@Override
	public int getCount() {
		return cards.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
}